#include "usermgr.h"'
#include <QDebug>
#define CHAT_PER_PAGE_COUNT 13
UserMgr::~UserMgr()
{
}

void UserMgr::SetToken(QString token)
{
    _token = token;
}

void UserMgr::SetUserInfo(std::shared_ptr<UserInfo> user_info)
{
    _user_info = user_info;
}

std::shared_ptr<UserInfo> UserMgr::GetUserInfo()
{
    return _user_info;
}

QString UserMgr::GetName()
{
    return _user_info->_name;
}

int UserMgr::GetUid()
{
    return _user_info->_uid;
}

QString UserMgr::GetIcon()
{
    return _user_info->_icon;
}

std::vector<std::shared_ptr<ApplyInfo> > UserMgr::GetApplyList()
{
    return _apply_list;
}

void UserMgr::AppendApplyList(QJsonArray array)
{
    for (int i=0;i<array.size();i++) {
        QJsonValue value = array[i];
        auto name = value["name"].toString();
        auto uid = value["uid"].toInt();
        auto icon = value["icon"].toString();
        auto nick = value["nick"].toString();
        auto sex = value["sex"].toInt();
        auto status = value["status"].toInt();
        _apply_list.push_back(std::make_shared<ApplyInfo>(
                                  uid,name,"",icon,nick,sex,status));
    }
}

void UserMgr::AppendFriendList(QJsonArray array)
{
    for (int i=0;i<array.size();i++) {
        QJsonValue value = array[i];
        auto name = value["name"].toString();
        auto uid = value["uid"].toInt();
        auto icon = value["icon"].toString();
        auto nick = value["nick"].toString();
        auto sex = value["sex"].toInt();
        auto desc = value["desc"].toString();
        auto back = value["back"].toString();
        auto info = std::make_shared<FriendInfo>(uid,name,nick,icon,sex,desc,back);
        _friend_list.push_back(info);
        _friend_map.insert(uid, info);
   }
}

bool UserMgr::AlreadyApply(int uid)
{
    for(auto item : _apply_list){
        if(item->_uid == uid){
            if(item->_status == 1){
                return true;
            }
            return false;
        }
    }
    qDebug()<<"alreadyApply can not find uid";
}

void UserMgr::AddApplyInfo(std::shared_ptr<AddFriendApply> apply)
{
    auto name = apply->_name;
    auto uid = apply->_from_uid;
    auto icon = apply->_icon;
    auto nick = apply->_nick;
    auto sex = apply->_sex;
    auto status = 0;
    _apply_list.push_back(std::make_shared<ApplyInfo>(
                              uid,name,"",icon,nick,sex,status));
}

std::vector<std::shared_ptr<FriendInfo> > UserMgr::GetChatListPerPage()
{
    std::vector<std::shared_ptr<FriendInfo>> friend_info;
    int begin = _chat_loaded;
    int end = begin + CHAT_PER_PAGE_COUNT;
    if(begin >= _friend_list.size()){
        return  friend_info;
    }
    if(end >= _friend_list.size()){
        friend_info = std::vector<std::shared_ptr<FriendInfo>>(_friend_list.begin() + begin, _friend_list.end());
        return friend_info;
    }
    friend_info = std::vector<std::shared_ptr<FriendInfo>>(_friend_list.begin() + begin, _friend_list.begin() + end);
    return friend_info;
}

bool UserMgr::IsLoadChatFin()
{
    if (_chat_loaded >= _friend_list.size()) {
        return true;
    }

    return false;
}

void UserMgr::UpdateChatLoadedCount()
{
    int begin = _chat_loaded;
    int end = begin + CHAT_PER_PAGE_COUNT;

    if (begin >= _friend_list.size()) {
        return ;
    }

    if (end > _friend_list.size()) {
        _chat_loaded = _friend_list.size();
        return ;
    }

    _chat_loaded = end;
}

std::vector<std::shared_ptr<FriendInfo> > UserMgr::GetConListPerPage()
{
    std::vector<std::shared_ptr<FriendInfo>> friend_info;
    int begin = _con_loaded;
    int end = begin + CHAT_PER_PAGE_COUNT;
    if(begin >= _friend_list.size()){
        return  friend_info;
    }
    if(end >= _friend_list.size()){
        friend_info = std::vector<std::shared_ptr<FriendInfo>>(_friend_list.begin() + begin, _friend_list.end());
        return friend_info;
    }
    friend_info = std::vector<std::shared_ptr<FriendInfo>>(_friend_list.begin() + begin, _friend_list.begin() + end);
    return friend_info;
}

void UserMgr::UpdateContactLoadedCount()
{
    int begin = _con_loaded;
    int end = begin + CHAT_PER_PAGE_COUNT;

    if (begin >= _friend_list.size()) {
        return ;
    }

    if (end > _friend_list.size()) {
        _con_loaded = _friend_list.size();
        return ;
    }

    _con_loaded = end;
}

bool UserMgr::IsLoadConFin()
{
    if(_con_loaded < _friend_list.size()){
        return false;
    }
    return true;
}

bool UserMgr::CheckFriend(int uid)
{
    auto iter = _friend_map.find(uid);
    if(iter == _friend_map.end()){
        return false;
    }

    return true;
}

void UserMgr::AddFriend(std::shared_ptr<AuthRsp> auth_rsp)
{
    auto friend_info = std::make_shared<FriendInfo>(auth_rsp);
    _friend_map[friend_info->_uid] = friend_info;
}

void UserMgr::AddFriend(std::shared_ptr<AuthInfo> auth_info)
{
    auto friend_info = std::make_shared<FriendInfo>(auth_info);
    _friend_map[friend_info->_uid] = friend_info;
}

std::shared_ptr<FriendInfo> UserMgr::GetFriendById(int uid)
{
    auto find_it = _friend_map.find(uid);
    if(find_it == _friend_map.end()){
        return nullptr;
    }

    return *find_it;
}

void UserMgr::AppendFriendChatMsg(int friend_id, std::vector<std::shared_ptr<TextChatData>> msgs)
{
    auto find_iter = _friend_map.find(friend_id);
    if(find_iter == _friend_map.end()){
        qDebug()<<"append friend uid  " << friend_id << " not found";
        return;
    }
    qDebug()<<"append friend uid  " << friend_id;
    find_iter.value()->AppendChatMsgs(msgs);
}

UserMgr::UserMgr():_user_info(nullptr),_chat_loaded(0),_con_loaded(0)
{

}
